package com.eerl.pdev;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ide.CopyPasteManager;
import org.jetbrains.annotations.NotNull;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.*;

public class EVarPrint extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {

        try {
            String string = Objects.requireNonNull(CopyPasteManager.getInstance().getContents()).getTransferData(DataFlavor.stringFlavor).toString();
            String ePrintStr = constructIoFormat(eVarSplit(string));
            CopyPasteManager.getInstance().setContents(new StringSelection(ePrintStr));
        } catch (UnsupportedFlavorException | IOException ex) {
            throw new RuntimeException(ex);
        }

    }


    public List<String> eVarSplit(String expression) {
        StringBuilder sb = new StringBuilder(10);
        Set<String> eVars = new HashSet<>(15);
        for(int i = 0; i < expression.length(); i ++) {
            char elem = expression.charAt(i);
            if (isValidEVarElem(elem)) {
                sb.append(elem);
            } else if (!sb.isEmpty()) {
                char firstElem = sb.charAt(0);
                if (('A' <= firstElem && firstElem <= 'Z') || (firstElem == '_')) {
                    String eVar = sb.toString();
                    // filter contain role var, too big! and not a '_' var
                    if (varFilter(eVar)) {
                        eVars.add(eVar);
                    }
                }
                sb = new StringBuilder(10);
            }
        }
        if (!sb.isEmpty()) {
            char firstElem = sb.charAt(0);
            if (('A' <= firstElem && firstElem <= 'Z') || (firstElem == '_')) {
                String eVar = sb.toString();
                // filter contain role var, too big! and not a '_' var
                if (varFilter(eVar)) {
                    eVars.add(eVar);
                }
            }
        }
        return eVars.stream().toList();
    }

    public boolean isValidEVarElem(char elem) {
        return elem == '_' || Character.isLetterOrDigit(elem);
    }

    public String constructIoFormat(List<String> eVars) {
        StringJoiner content = new StringJoiner(": ~w || ", "", ": ~w ||");
        StringJoiner vars = new StringJoiner(", ");
        eVars.forEach(eVar -> {
            content.add(eVar);
            vars.add(eVar);
        });
        String prefix = MySettingsState.getInstance().myValue;
        return String.format("?%s(\"%s\", [%s]),", prefix, content, vars);
    }

    private boolean varFilter(String eVar) {
        if (eVar.matches("_*") || eVar.matches("_*[a-z].*")) {
            return false;
        }
        if (eVar.matches(MySettingsState.getInstance().INCLUDE_VAR_FILTER_PATTERN)) {
            return true;
        } else if (eVar.matches(MySettingsState.getInstance().EXCLUDE_VAR_FILTER_PATTERN)) {
            // System.out.println("过滤 => " + eVar);
            return false;
        }
        return true;
    }

}

